//# sourceURL=http://uMapApp/Module/Maintain/Maintain
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
    , "../StakeNum/StakeNum"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum
) {

    function Maintain(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        _this.stakeNum = StakeNum.transform(_this["桩号"]);

        _this.title =
            _this.undefinedText(_this["病害主类型"], _this["病害主类型"])
            + _this.undefinedText(_this["病害子类型"], " / " + _this["病害子类型"])
            + _this.undefinedText(_this["病害程度"], " / " + _this["病害程度"])
        ;

        _this.subtitle =
            _this["位置"]
            + " / " + _this["朝向"]
            + " / " + _this["桩号"]
            + " / " + _this["车道"]
        ;

        _this.searchKey =
            _this.undefinedText(_this["位置"])
            + " " + _this.undefinedText(_this["病害主类型"])
            + " " + _this.undefinedText(_this["病害子类型"])
            + " " + _this.undefinedText(_this["病害程度"])
            + " " + _this["朝向"]
            + " " + _this["桩号"]
            + " " + _this["车道"];

        _this.sortKey =
            (_this["朝向"] === "上行" ? 1e7 : 2e7)
            + _this.stakeNum.distance
            + (_this["rowId"] === undefined ? 0 : _this["车道序号"]) * 0.1;

        _this.speechText =

            _this.undefinedText(_this["巡查时间"], "于" + _this["巡查时间"])
            + _this.undefinedText(_this["位置"], ",在" + _this["位置"])
            + _this.undefinedText(_this["朝向"], "," + _this["朝向"])
            + _this.undefinedText(_this["车道"], "," + _this["车道"])
            + _this.undefinedText(_this["桩号"], "," + _this["桩号"])
            + "处发现"
            + _this.undefinedText(_this["病害程度"], _this["病害程度"] + "级别")
            + _this.undefinedText(_this["病害子类型"], _this["病害子类型"] + "。")
            + _this.undefinedText(_this["紧急程度"], "紧急程度：" + _this["紧急程度"])
            + _this.undefinedText(_this["状态"], "当前状态：" + _this["状态"])
        ;

        if (_this.damageTypeDic) {
            $.each(_this.damageTypeDic, function (i, damageMainType) {
                if (damageMainType.title === _this["病害主类型"]) {
                    _this.damageMainType = damageMainType;

                    $.each(damageMainType.children, function (j, damageType) {
                        if (damageType.title === _this["病害子类型"]) {
                            _this.damageType = damageType;
                        }
                    });
                }
            });
        }

        if (_this.dic && _this.dic.damageDegrees && _this.dic.damageDegrees[_this["病害程度"]]) {
            _this.damageDegree = _this.dic.damageDegrees[_this["病害程度"]];
        }

        if (_this.dataObj && _this.dataObj.position) {
            _this.position = new uMap.Position(_this.dataObj.position);
        } else if (_this.positionHeading) {
            _this.position = new uMap.Position(_this.positionHeading.position);
            _this.heading = _this.positionHeading.heading;
        }

        if (_this.position) {
            _this.render();
        }

    }

    var _class = Maintain;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "render"       : {
            value: function () {

                var _this = this;

                var primitiveLevel = {
                    point    : new uMap.DistanceCondition([0, 1e5]),
                    label    : new uMap.DistanceCondition([0, 1e5]),
                    billboard: new uMap.DistanceCondition([0, 1e5])
                };

                var primitiveOption = {
                    point    : {
                        pixelSize                 : 5
                        ,
                        color                     : new uMap.Color(_this.damageDegree && _this.damageDegree.color || "#fff")
                        , outlineColor            : new uMap.Color(["#000"])
                        , outlineWidth            : 2
                        , scaleByDistance         : primitiveLevel.point.scaleByDistance
                        , translucencyByDistance  : primitiveLevel.point.translucencyByDistance
                        , distanceDisplayCondition: primitiveLevel.point.distanceDisplayCondition
                        , disableDepthTestDistance: Number.POSITIVE_INFINITY
                    },
                    label    : {
                        font                      : "16px Microsoft Yahei"
                        , style                   : uEarth.LabelStyle.FILL_AND_OUTLINE
                        , fillColor               : new uMap.Color(["#fff"])
                        , outlineColor            : new uMap.Color(["#000"])
                        , outlineWidth            : 5.0
                        , horizontalOrigin        : uEarth.HorizontalOrigin.CENTER
                        , verticalOrigin          : uEarth.VerticalOrigin.TOP
                        , pixelOffset             : uEarth.Cartesian2.fromArray([0, 10])
                        , scaleByDistance         : primitiveLevel.label.scaleByDistance
                        , translucencyByDistance  : primitiveLevel.label.translucencyByDistance
                        , distanceDisplayCondition: primitiveLevel.label.distanceDisplayCondition
                        , disableDepthTestDistance: Number.POSITIVE_INFINITY
                    },
                    billboard: {
                        verticalOrigin            : uEarth.VerticalOrigin.BOTTOM
                        , scaleByDistance         : primitiveLevel.billboard.scaleByDistance
                        , translucencyByDistance  : primitiveLevel.billboard.translucencyByDistance
                        , distanceDisplayCondition: primitiveLevel.billboard.distanceDisplayCondition
                        , disableDepthTestDistance: Number.POSITIVE_INFINITY
                    }
                };

                _this.viewport = new ViewPort({
                    "center": _this.position,
                    "hpr"   : {
                        "heading": _this.heading || 0,
                        "pitch"  : -20,
                        "range"  : 50
                    }
                });

                _this.point = new uMap.EventObject(_this.module.layer.pointPrimitives.add(
                    $.extend({}, primitiveOption.point,
                        {
                            position: _this.position.cartesian
                        })
                ));

                _this.point.on("click", function () {
                    _this.trigger("click");
                });

                _this.label = new uMap.EventObject(_this.module.layer.labelPrimitives.add(
                    $.extend({}, primitiveOption.label,
                        {
                            position: _this.position.cartesian
                            , text  : _this.title
                            , show  : false
                        })
                ));

                _this.label.on("click", function () {
                    _this.trigger("click");
                });

                _this.billboard = new uMap.EventObject(_this.module.layer.billboardPrimitives.add(
                    $.extend({}, primitiveOption.billboard,
                        {
                            position: _this.position.cartesian
                            , image : _this.app.icons.getIconMarker(_this.icon)
                            , show  : false
                        })
                ));

                _this.billboard.on("click", function () {
                    _this.trigger("click");
                });
            }
        },
        "undefinedText": {
            value: function (value, trueReturn, falseReturn) {
                return (value !== undefined) ? (trueReturn || value) : (falseReturn || "");
            }
        },
        "open"         : {
            value: function () {
                if (!this.isOpened) {
                    // this.layer && this.layer.open();

                    var _this = this;

                    if (_this.position) {
                        _this.point.object.show = true;
                        _this.label.object.show = false;
                        _this.billboard.object.show = false;
                    }


                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close"        : {
            value: function () {
                if (this.isOpened) {
                    // this.layer && this.layer.close();

                    var _this = this;

                    if (_this.position) {
                        _this.point.object.show = false;
                        _this.label.object.show = false;
                        _this.billboard.object.show = false;
                    }

                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle"       : {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "speak"        : {
            value: function () {
                var _this = this;
                if (_this.app && _this.app.speaker && _this.speechText) {
                    _this.app.speaker.start(_this.speechText);
                }
            }
        },
        "active"       : {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        if (_this.position) {

                            // var terrainHeightPosition = _this.app.earth.scene.clampToHeight(_this.position.cartesian);
                            //
                            // if (terrainHeightPosition) {
                            //     _this.position = new uMap.Position(terrainHeightPosition);
                            //     _this.layer.viewport.center = _this.position;
                            //     _this.marker.position = terrainHeightPosition;
                            // }

                            // _this.layer.active();
                            // _this.marker.billboard.show = true;
                            // _this.marker.label.show = true;
                            // _this.marker.point.color = new uMap.Color(["#fff"]);

                            // _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.icon, true);

                            _this.label.object.show = true;
                            _this.billboard.object.show = true;

                        }

                        _this.trigger("active");

                    } else {

                        if (_this.position) {
                            // _this.marker.billboard.show = false;
                            // _this.marker.label.show = false;

                            // _this.marker.point.color = new uMap.Color([_this.damageType.color || "#fff"]);

                            // _this.marker.label.fillColor = new uMap.Color(["#fff"]);
                            // _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.icon);
                            // _this.layer.close();

                            _this.label.object.show = false;
                            _this.billboard.object.show = false;
                        }

                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }


            }
        }
    });

    return _class;

});